package com.sugon.controller.module;

//import cn.afterturn.easypoi.entity.vo.TemplateExcelCloneConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.date.DateUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleSpDemandPlanEntity;
import com.sugon.service.ModuleSpDemandPlanService;
import com.sugon.service.SysConfigService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 模具型芯需求计划Controller
 *
 * @author sugon
 * @date 2022-07-11 17:08:50
 */
@Controller
@RequestMapping("modulespdemandplan")
public class ModuleSpDemandPlanController {

    @Autowired
    private ModuleSpDemandPlanService moduleSpDemandPlanService;
    @Autowired
    private SysConfigService configService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleSpDemandPlanEntity> moduleSpDemandPlanList = moduleSpDemandPlanService.queryList(query);
        int total = moduleSpDemandPlanService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleSpDemandPlanList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/export")
    public void export(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> queryParam) {

        // 设置导出EXCEL模板名称
//        modelMap.put(TemplateExcelCloneConstants.FILE_NAME, "型芯制作明细导出_" + DateUtil.format(DateUtil.date(), "yyyyMMdd"));

        TemplateExportParams params = new TemplateExportParams(configService.getPoiTemplatePath() + "moldSpFabricationItemTemplate.xls");
        // 机械使用派遣单
        params.setSheetNum(new Integer[]{0});

//        modelMap.put(TemplateExcelCloneConstants.PARAMS, params);
        // 获取模板数据
        Map<Integer, List<Map<String, Object>>> templateData = moduleSpDemandPlanService.prepareExportTemplateData(queryParam);
//        modelMap.put(TemplateExcelCloneConstants.MAP_DATA, templateData);
//        PoiBaseView.render(modelMap, request, response, TemplateExcelCloneConstants.EASYPOI_TEMPLATE_EXCEL_CLONE_VIEW);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleSpDemandPlanEntity moduleSpDemandPlan = moduleSpDemandPlanService.queryObject(id);

        return R.ok().put("moduleSpDemandPlan", moduleSpDemandPlan);
    }

    /**
     * 保存
     */
    @AutoLog("模具型芯需求计划--保存")
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody ModuleSpDemandPlanEntity moduleSpDemandPlan) {
        moduleSpDemandPlanService.save(moduleSpDemandPlan);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("模具型芯需求计划--修改")
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody ModuleSpDemandPlanEntity moduleSpDemandPlan) {
        moduleSpDemandPlanService.update(moduleSpDemandPlan);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具型芯需求计划--删除")
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleSpDemandPlanService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleSpDemandPlanEntity> list = moduleSpDemandPlanService.queryList(params);

        return R.ok().put("list", list);
    }
}
